<?php
//---------------------------------------------------------------------------------------------------------------------------
   include_once($config['app_dir'] . '/includes/object.class.php');
   //------------------------------------------------------------------------------------------------------------------------
        class objFormParams  implements iDebug, ArrayAccess{
                protected $prefix='data_';
                protected $param_items;
                protected $operation;
                protected $method;

                //-------------------------------------------------------------------------------------------
                function __construct($method='both', $nprefix='data_') {
                        $this->prefix = $nprefix;
                        $this->param_items = array();
                        $this->method = strtolower($method);

                        $this->build();
                } //constructor

                function setPrefix($nprefix) {
                        $this->prefix = $nprefix;
                } //setPrefix

                function getPrefix() {
                        return $this->prefix;
                } //getPrefix
                //-------------------------------------------------------------------------------------------
                function build(){
                        $i=0;
                        if('both' == $this->method)  $array_temp = array_merge($_POST, $_GET);
                           elseif('get' == $this->method) $array_temp = &$_GET;
                              elseif('post' == $this->method) $array_temp = &$_POST;

                        if ( $this->prefix ) {
                          foreach ($array_temp as $key => $val) {
                            if (strstr($key, $this->prefix)) {
                                $key = substr($key, strlen($this->prefix));
                                if ( '' == $val ) $val = 'null';
                                $this->param_items[$i]['name'] = $key;
                                $this->param_items[$i++]['value'] = $val;
                             }//if
                             elseif ('operation' == $key)  $this->operation = strtolower($val);
                          } //foreach
                        } else {
                          foreach ($array_temp as $key => $val) {
                             if ( '' == $val ) $val = 'null';
                             $this->param_items[$i]['name']=$key;
                             $this->param_items[$i++]['value']=$val;
                          } //foreach
                        } //else

                        return $this->param_items;
                } //build

                function _prefix_value($value) {
                   $key = false;
                   if (strstr($value, $this->prefix) )
                     $key = substr($value, strlen($this->prefix));
                   return $key;
                }//_prefixkey

                function isEmpty() {
                        return  0==$this->length();
                } //isEmpty

                function length() {
                       return  count($this->param_items);
                } //length

                function isItem($nname) {
                         for ($i=0; $i < count($this->param_items); $i++)
                            if ($this->param_items[$i]['name'] == $nname) return true;
                        return false;
                } //isItem

                function getItemIndex($index) {
                        return $this->param_items[$index];
                } //getItemIndex

                function getItem($nname) {
                        for ($i=0; $i < count($this->param_items); $i++)
                          if ($this->param_items[$i]['name']==$nname)
                             return $this->param_items[$i]['value'];
                        return false;
                } //getItem

                function delItem($nname) {
                        $params = array();  $c = 0;
                        for ($i=0; $i < count($this->param_items); $i++)
                          if ($this->param_items[$i]['name'] != $nname) {
                               $params[$c]['name'] = $this->param_items[$i]['name'];
                               $params[$c++]['value'] = $this->param_items[$i]['value'];
                          } //if
                        $this->param_items = $params;
                } //delItem

                function addItem($nname, $nvalue) {
                        $flag=false;
                        for ($i=0; $i < count($this->param_items); $i++)
                                if ($this->param_items[$i]['name'] == $nname)        {
                                        $this->param_items[$i]['value'] = $nvalue;
                                        $flag=true;
                                }//if
                        if (!$flag) {
                                $c=count($this->param_items);
                                $this->param_items[$c]['name'] = $nname;
                                $this->param_items[$c]['value'] = $nvalue;
                        } //if new
                } //addItem

                function getOperation() {
                        return $this->operation;
                } //getOperation

                function getAssocArray($forcenulls=false) {
                        $result = array();
                        for ($i=0; $i < count($this->param_items); $i++) {
                            if ( !$forcenulls ) {
                               if ('null' != $this->param_items[$i]['value'])
                                  $result[$this->param_items[$i]['name']] = $this->param_items[$i]['value'];
                            } else
                               $result[$this->param_items[$i]['name']] = $this->param_items[$i]['value'];
                        } //for
                        return $result;
                } //getAssocArray

                function getAssocArrayWithPrefix($prefix, $forcenulls=false) {
                        $result = array();
                        for ($i=0; $i < count($this->param_items); $i++) {
                            if ( !$forcenulls ) {
                               if ('null' != $this->param_items[$i]['value'] &&
                                   strstr($this->param_items[$i]['name'], $prefix) )
                                     $result[substr($this->param_items[$i]['name'], strlen($this->prefix))] =
                                        $this->param_items[$i]['value'];
                            } else {
                               if ( strstr($this->param_items[$i]['name'], $prefix) )
                                     $result[substr($this->param_items[$i]['name'], strlen($this->prefix))] =
                                        $this->param_items[$i]['value'];
                            }
                        } //for
                        return $result;
                } //getAssocArrayWithPrefix


                function dump() {
                        echo 'POST Variables dump: <br>';
                        foreach ($this->param_items as $param) {
                           echo $param['name'].'=>';
                           var_dump($param['value']);
                           echo '<br>';
                        }
                } //dump
                //-------------------------------------------------------------------------------------------
                // ArrayAccess
                //-------------------------------------------------------------------------------------------
                function offsetSet($key, $value) {
                        $this->addItem($key, $value);
                } //offsetSet

                function offsetGet($key) {
                        return $this->getItem($key);
                } //offsetGet

                function offsetUnset($key) {
                        $this->delItem($key);
                } //offsetUnset

                function offsetExists($offset) {
                        return $this->isItem($offset);
                } //offsetExists

        } //class objFormParams
//---------------------------------------------------------------------------------------------------------------------------
        class objFormCompositeParams  implements iDebug, ArrayAccess{
                protected $prefix='data_';
                protected $postfix=false;
                protected $param_items;
                protected $operation;
                protected $method;

                function __construct($method='both', $nprefix='data_', $npostfix=false, $forcenull=false) {
                        $this->prefix = $nprefix;
                        $this->postfix = $npostfix;
                        $this->param_items = array();
                        $this->method = strtolower($method);

                        $this->build($forcenull);
                } //constructor

                function setPrefix($nprefix) {
                        $this->prefix = $nprefix;
                } //setPrefix

                function getPrefix() {
                        return $this->prefix;
                } //getPrefix

                function getAssocArray($forcenull=false, $mode='plain') {
                    if ('plain' == $mode) {
                       $result =array();
                       if ( !$this->isEmpty() )
                          foreach ($this->param_items as $pindex => $pvalue)
                           if (is_array($pvalue) ) $result = array_merge($result, $pvalue);

                       return $result;
                    } else     return $this->param_items;

                } //getAssocArray

                function build($forcenull=false){
                          if('both' == $this->method)  $array_temp = array_merge($_POST, $_GET);
                           elseif('get' == $this->method) $array_temp = &$_GET;
                              elseif('post' == $this->method) $array_temp = &$_POST;

                        $index = 0;
                        foreach ($array_temp as $key => $value) {
                            $key = $this->_prefix($key, $prefix);

                            if ( 'operation' == $key) {
                                 $this->operation = strtolower($value);
                                 continue;
                            }
                            if ($this->prefix && false === $prefix) continue;

                            $key = $this->_postfix($key, $master);
//                            if ($this->postfix && false === $postfix) continue;

                            if (!$forcenull && ('' == $value || ( $this->postfix && '' == $master  )) ) continue;

                            if ( '' == $value ) $value = 'null';
                            if ( '' == $master ) $master = 'null';

                            if ( 'null' === $master && !$this->postfix )  $master = $index++;
                            $this->param_items[$master][$key] = $value;
                        }
                        return $this->param_items;
                } //build

                function _prefix(&$str, &$prefix) {
                    $prefix = false;
                    if( $this->prefix &&  strstr($str, $this->prefix) ) {
                        $i = strpos($str, $this->prefix);
                        $prefix = substr($str, $i, strlen($this->prefix));
                        return $str = substr($str, $i+strlen($this->prefix));
                    }
                     else return $str;
                } //_prefix

                function _postfix(&$str, &$postfix) {
                    $postfix = false;
                    if ($this->postfix && strstr($str, $this->postfix) ) {
                       $i = strpos($str, $this->postfix);
                       $postfix = substr($str, $i+strlen($this->postfix));
                       return $str = substr($str, 0, $i);
                    } else return $str;

                } //_prefix

                function isEmpty() {
                        return  0===$this->length();
                } //isEmpty

                function length() {
                       return  count($this->param_items);
                } //length

                function getOperation() {
                        return $this->operation;
                } //getOperation

                function isItem($index) {
                        foreach ($this->param_items as $pindex => $pvalue)
                            if ($pindex == $index) return true;
                        return false;
                } //isItem

                function getItem($index) {
                        foreach ($this->param_items as $pindex => $pvalue)
                            if ($pindex == $index) return $pvalue;
                        return false;
                } //getItem

                function delItem($index) {
                        $params = array();
                        foreach ($this->param_items as $pindex => $pvalue)
                          if ($pindex != $index) $params[$pindex] = $pvalue;

                        $this->param_items = $params;
                } //delItem

                function addItem($nindex, $nvalue) {
                        $this->param_items[$nindex] = $nvalue;
                } //addItem

                function slice($field=false) {
                        $result = array();
                        foreach ($this->param_items as $pindex => $pvalue)
                          foreach ($pvalue as $name => $value) {

                             if( false!==$field && $field == $name) {
                                $result[] =  array($pindex => $value);
                             }
                          }
                        return $result;
                } //slice

                function dump() {
                        echo 'POST Variables dump: <br>';
                        foreach ($this->param_items as $pindex => $pvalue) {
                           echo $pindex.'=>';
                           var_dump($pvalue);
                           echo '<br>';
                        }
                } //dump

                //-------------------------------------------------------------------------------------------
                // ArrayAccess
                //-------------------------------------------------------------------------------------------
                function offsetSet($key, $value) {
                        $this->addItem($key, $value);
                } //offsetSet

                function offsetGet($key) {
                        return $this->getItem($key);
                } //offsetGet

                function offsetUnset($key) {
                        $this->delItem($key);
                } //offsetUnset

                function offsetExists($offset) {
                        return $this->isItem($offset);
                } //offsetExists


        } //class objFormCompositeParams
//---------------------------------------------------------------------------------------------------------------------------

?>